<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
// 
//  user_model.php
//  www.6block.com
//  
//  Created by Will_Lee on 2011-05-21.
//  Copyright 2011 lishuzu. All rights reserved.
// 


class User_model extends CI_Model
{
		function __construct(){
			parent::__construct();
	 		$this->load->model('common/security_model');
		}
		
		function add($data){
			if ($data) {
				$this->db->insert('user', $data);
				return $this->db->insert_id();
			}
			return false;
		}
		
		//智能提取用户信息 如果是本人返回session中的值
		function get_user_smart($uid){
			$userinfo = array();
			if ($this->security_model->checkself($uid)) {
				return $this->session->userdata('user');
			}else{
				$userinfo = $this->getuser('uid', $uid);
				return $userinfo;
			}
		}
		
		function getuser($key, $value){
			if ($key) {
				$this->db->where($key, $value);
				$query = $this->db->get('user');
				$data = $query->row_array();
				if ($data) {
					$data['avatar'] = avatar($data['uid'], 'big');
					$data['avatar_small'] = avatar($data['uid'], 'small');
					// 换行
					$data['intro'] = str_replace(array("\n", "\r\n"), "<br/>", $data['intro']);
					return $data;
				}
			}
			return false;
		}
		
		function update($uid, $data){
			if ($uid) {
				$wheresql = array('uid' => $uid);
				$this->db->where($wheresql);
				$this->db->update('user', $data);
				return true;
			}
			return false;
		}
		
		function upload_avatar($uid){
			if ($uid && $_FILES['avatar']['name']) {
				$home = get_home($uid);
				if(!is_dir(FCPATH.'upload/avatar/'.$home)) {
					set_home($uid, FCPATH.'upload/avatar/');
				}
				$avatartype = 'virtual';
				$bigavatarfile = FCPATH.'upload/avatar/'.get_avatar($uid, 'big', $avatartype);
				$middleavatarfile = FCPATH.'upload/avatar/'.get_avatar($uid, 'middle', $avatartype);
				$smallavatarfile = FCPATH.'upload/avatar/'.get_avatar($uid, 'small', $avatartype);
				$originavatarfile = substr(sprintf("%09d", abs(intval($uid))), -2)."_avatar_origin.jpg";
				$uploadconfig =  array(
							'formname' => 'avatar',
							'upload_path' => FCPATH.'upload/avatar/'.$home,
							'allowed_types' => 'gif|jpg|png|jpeg',
							'file_name' => $originavatarfile,
							'overwrite' => true,
							'max_size' => '1024',
							'max_width' => '0',
							'max_height' => '0',
							'max_filename' => 0,
							'encrypt_name' => '',
							'remove_spaces' => TRUE,
				);
				
				$this->load->library('upload', $uploadconfig);
				if (!$this->upload->do_upload($uploadconfig['formname'])){
					$uploadinfo = array('error' => $this->upload->display_errors());
					showmessage('上传图片失败'.$uploadinfo['error']);
				}else{
					//头像已经上传
					$this->db->query("UPDATE ".$this->db->dbprefix('user')." SET avatar = 1 WHERE uid='".$uid."'");
					$uploadinfo =  $this->upload->data();
				}
				if (!isset($uploadinfo['error'])) {
					// 缩略图
					$this->load->library('image_lib');
					$config['image_library'] = 'gd2';
					$config['source_image'] = FCPATH.'upload/avatar/'.get_avatar($uid, 'origin', $avatartype);
					//大图 180 180
					$config['new_image'] = $bigavatarfile;
					$config['width'] = 180;
					$config['height'] = 180;
					$this->image_lib->initialize($config);
					$res = $this->image_lib->resize();
					
					// 中图 120 120
					$config['new_image'] = $middleavatarfile;
					$config['width'] = 120;
					$config['height'] = 120;
					$this->image_lib->initialize($config);
					$res = $this->image_lib->resize();
					// 小图 50 50
					$config['new_image'] = $smallavatarfile;
					$config['width'] = 50;
					$config['height'] = 50;
					$this->image_lib->initialize($config);
					$res = $this->image_lib->resize();
					//删除原图片
					@unlink(FCPATH.'upload/avatar/'.get_avatar($uid, 'origin', $avatartype));
					return true;
				}else{
					return false;
				}
			}
		}
		// 用户裁剪头像小图片
		function resize_avatar($uid, $size){
			if ($uid && $size) {
				// 加载图像类
				$this->load->library('image_lib');
				
				list($x, $y, $w, $h) = explode('_', $size);
				$avatartype = 'virtual';
				$bigavatarfile = FCPATH.'upload/avatar/'.get_avatar($uid, 'big', $avatartype);
				$smallavatarfile = FCPATH.'upload/avatar/'.get_avatar($uid, 'small', $avatartype);
				// 裁剪
				$this->load->library('image_lib');
				$config['image_library'] = 'gd2';
				$config['source_image'] = $bigavatarfile;
				$config['new_image'] = $smallavatarfile;
				$config['maintain_ratio'] = false;
				$config['width'] = $w;
				$config['height'] = $h;
				$config['x_axis'] = intval($x);
				$config['y_axis'] = intval($y);
				$this->image_lib->initialize($config);
				$this->image_lib->crop();
				// 缩放到50
				$config['source_image'] = $smallavatarfile;
				$config['new_image'] = $smallavatarfile;
				$config['width'] = 50;
				$config['height'] = 50;
				$this->image_lib->initialize($config);
				$res = $this->image_lib->resize();
				
				if ($res) {
					return true;
				}else{
					return false;
				}
			}
		}
		
		// 删除头像
		function delete_avatar($uid){
			if ($uid) {
				$bigavatar = avatar($uid, 'big', true);
				$middleavatar = avatar($uid, 'middle', true);
				$smallavatar = avatar($uid, 'small', true);
				@unlink($bigavatar);
				@unlink($middleavatar);
				@unlink($smallavatar);
			}
		}
		
		// 批量更新用户资料
		function updates($uids, $data){
			if ($uids) {
				$this->db->where_in('uid', $uids);
				$this->db->update('user', $data);
				return true;
			}
			return false;
		}
}
?>